/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */
package cheat.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Represents a current user within the system.
 *
 * @author Keith Shie
 */
@Entity
@Table(name = "user_sess")
@NamedQueries(
  {
    @NamedQuery(name = "UserSession.findAll",
                query = "SELECT u FROM UserSession u"),
    @NamedQuery(name = "UserSession.findBySessionId",
                query = "SELECT u FROM UserSession u WHERE u.sessionId = :sessionId"),
    @NamedQuery(name = "UserSession.findByCreateTimeStamp",
                query = "SELECT u FROM UserSession u WHERE u.createTimeStamp = :createTimeStamp"),
    @NamedQuery(name = "UserSession.findByLastUseTimeStamp",
                query = "SELECT u FROM UserSession u WHERE u.lastUseTimeStamp = :lastUseTimeStamp")})
public class UserSession implements Serializable {

  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @SequenceGenerator(name="user_sess_seq", sequenceName="user_sess_sess_id_seq", allocationSize=1)
  @GeneratedValue( strategy=GenerationType.SEQUENCE,generator="user_sess_seq")
  @Column(name = "sess_id", nullable = false)
  private Integer sessionId;
  @Basic(optional = false)
  @Column(name = "create_dt", nullable = false)
  @Temporal(TemporalType.TIMESTAMP)
  private Date createTimeStamp;
  @Basic(optional = false)
  @Column(name = "last_use_dt", nullable = false)
  @Temporal(TemporalType.TIMESTAMP)
  private Date lastUseTimeStamp;
  @JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
  @ManyToOne(optional = false,fetch=FetchType.EAGER)
  private RegisteredUser user;

  public UserSession() {
  }

  public UserSession(Integer sessId) {
    this.sessionId = sessId;
  }

  public UserSession(Integer sessId, Date createDt, Date lastUseDt) {
    this.sessionId = sessId;
    this.createTimeStamp = createDt;
    this.lastUseTimeStamp = lastUseDt;
  }

  public Integer getSessionId() {
    return sessionId;
  }

  public void setSessionId(Integer sessId) {
    this.sessionId = sessId;
  }

  public Date getCreateTimeStamp() {
    return createTimeStamp;
  }

  public void setCreateTimeStamp(Date createDt) {
    this.createTimeStamp = createDt;
  }

  public Date getLastUseTimeStamp() {
    return lastUseTimeStamp;
  }

  public void setLastUseTimeStamp(Date lastUseDt) {
    this.lastUseTimeStamp = lastUseDt;
  }

  public RegisteredUser getUser() {
    return user;
  }

  public void setUser(RegisteredUser userId) {
    this.user = userId;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (sessionId != null ? sessionId.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof UserSession)) {
      return false;
    }
    UserSession other = (UserSession) object;
    if ((this.sessionId == null && other.sessionId != null) || (this.sessionId != null && !this.sessionId.equals(other.sessionId))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "cheat.entities.UserSession[sessId=" + sessionId + "]";
  }
}
